//**********************************************************
//    File Name: Client.cs
//    Author: Team Motif
//    Date Created : 04/13/2007
//    Current Owner: Team Motif 
//                                                                            
//    Purpose: 
//    This class provides the functions needed to create, update, access, and delete a 
//    client.
//
//	  Copyright 2007
//**********************************************************

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Library
{
    public class Client : Person
    {

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Default constructor
        /// -------------------------------------------------------------------------------
        /// </summary>
        public Client()
        {
            mClientSummary = new ClientSummary();
            mClientAssessment = new ClientAssessment();

            mCharts = new ArrayList();
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Alternative constructor for a client
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <param name="firstName"></param>
        /// <param name="lastName"></param>
        public Client(String firstName, String lastName)
        {
            this.SetName(firstName, lastName);

            mClientSummary = new ClientSummary();
            mClientAssessment = new ClientAssessment();

            mCharts = new ArrayList();
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Full constructor for a Client
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <param name="firstName"></param>
        /// <param name="lastName"></param>
        /// <param name="dateOfBirth"></param>
        public Client(String firstName, String lastName, DateTime dateOfBirth)
        {
            this.SetName(firstName, lastName);
            this.SetDateOfBirth(dateOfBirth);

            mClientSummary = new ClientSummary();
            mClientAssessment = new ClientAssessment();

            mCharts = new ArrayList();
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Static global method which will return a Client based on a Client ID.  Will return
        /// Null if Client is not found
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <param name="ClientId"></param>
        /// <returns></returns>
        public static Client GetClientByID(long ClientId)
        {
            Client myClient = new Client();
            System.Object lockThis = new System.Object();
            lock (lockThis)
            {
                myClient = DBUtil.ReadClientInfoByID(ClientId);
            }
            return myClient;
        }
        

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Static global method which will return a list of clients based on a match of first
        /// and last name.  It is up to the caller to verity the list is not empty.
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <param name="firstName"></param>
        /// <param name="lastName"></param>
        /// <returns></returns>
        public static ArrayList GetClientList(string firstName, string lastName)
        {
            ArrayList ClientList = new ArrayList();
            System.Object lockThis = new System.Object();
            lock (lockThis)
            {
                ClientList = DBUtil.ReadClientList(firstName, lastName);
            }
             
            return ClientList;
        }
        /// <summary>
        /// -------------------------------------------------------------------------------
        /// This function will get the ClientNeeds and Weekely hours from the database
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <param name="ClientID"></param>
        /// <returns></returns>
        public static ArrayList GetClientNeedsAndWeeklyHours(long ClientID)
        {
            ArrayList ClientList = new ArrayList();
            System.Object lockThis = new System.Object();
            lock (lockThis)
            {
                ClientList = DBUtil.ReadClientNeedsAndWeekelyHours(ClientID);
            }
            return ClientList;
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// This function is used to delete the details of a client from the database
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <param name="ClientID"></param>
        /// <returns></returns>
        public static bool DeleteClients(long ClientID, String tmpFirstName, String tmpLastName)
        {
            bool ReturnVal = false;
            System.Object lockThis = new System.Object();
            lock (lockThis)
            {
                ReturnVal = DBUtil.DeleteClientsFromDB(ClientID, tmpFirstName, tmpLastName);
            }
            return ReturnVal;
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// This function will get the remianing hours (to be scheduled) for the client from the database
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <param name="ClientID"></param>
        /// <returns></returns>
        public static int GetClientRemainingHours(long ClientID, String szDate, String szNextdate)
        {
            int RemHours;
            System.Object lockThis = new System.Object();
            lock (lockThis)
            {
                RemHours = DBUtil.ReadClientRemainingHours(ClientID, szDate, szNextdate);
            }
            return RemHours;
        }

        
        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Static global method which will return a Client based on name and date of birth.
        /// Will Return null if client is not found
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <param name="firstName"></param>
        /// <param name="lastName"></param>
        /// <param name="dateOfBirth"></param>
        /// <returns></returns>
        public static Client GetClientByMatch(String firstName, String lastName, DateTime dateOfBirth)
        {
            Client newClient = null;

            System.Object lockThis = new System.Object();
            lock (lockThis)
            {

                //Connection to the database. Needs to do this in seperate File */
                SqlConnection conn = new SqlConnection("Initial Catalog=EHNW;Data Source=localhost;Integrated Security=True;");
                SqlDataReader rdr = null;

                try
                {
                    // 2. Open the connection
                    conn.Open();

                    // 3. Pass the connection to a command object
                    SqlCommand cmd = new SqlCommand("select * from User", conn);

                    // get query results
                    rdr = cmd.ExecuteReader();

                    // print the CustomerID of each record
                    while (rdr.Read())
                    {
                        Console.WriteLine(rdr[0]);
                    }
                }
                catch (SqlException exceptionMsg)
                {
                    LOG.error("Client::GetClientByMatch Exception handled: " + exceptionMsg.ToString());
                }
                finally
                {
                    // close the reader
                    if (rdr != null)
                    {
                        rdr.Close();
                    }

                    // 5. Close the connection
                    if (conn != null)
                    {
                        conn.Close();
                    }
                }
            }
            return newClient;
        }

        
        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Static global method which will store a Client.  Returns true/false for success
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public static bool SaveClient(Client tmpClient)
        {
            System.Object lockThis = new System.Object();
            bool ReturnVal = false;
            lock (lockThis)
            {
                ReturnVal = DBUtil.SaveClientToDataBase(tmpClient);
            }
            return ReturnVal;
        }

        //public static bool DeleteClient(Client client)
        /// <summary>
        /// Retrieves all the Clients in the database in an ArrayList collection
        /// </summary>
        /// <returns></returns>
        public static ArrayList GetAllClients()
        {
            ArrayList clientList = new ArrayList();

            System.Object lockThis = new System.Object();
            lock (lockThis)
            {

                //Connection to the database. Needs to do this in seperate File */
                SqlConnection conn = new SqlConnection("Initial Catalog=EHNW;Data Source=localhost;Integrated Security=True;");
                SqlDataReader rdr = null;

                try
                {
                    // Open the connection
                    conn.Open();

                    //Pass the connection to a command object
                    SqlCommand cmd = new SqlCommand("select * from User where UserType=\"C\"", conn);

                    // get query results
                    rdr = cmd.ExecuteReader();

                    // print results of the query.  
                    // TODO....NEED TO CREATE A CLIENT OUT OF THIS.
                    while (rdr.Read())
                    {
                        Client client = new Client();

                        Console.WriteLine(rdr[0].ToString());
                    }
                }
                catch (SqlException what)
                {
                    LOG.error("Client::GetClientByMatch Exception handled: " + what.ToString());
                }
                finally
                {
                    // close the reader
                    if (rdr != null)
                    {
                        rdr.Close();
                    }

                    // 5. Close the connection
                    if (conn != null)
                    {
                        conn.Close();
                    }
                }
            }
            return clientList;
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Returns the client ID
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <returns></returns>
        public long GetClientId()
        {
            return this.GetUserID();
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Returns the client assessment class
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <returns></returns>
        public ClientAssessment GetAssessment()
        {
            return mClientAssessment;
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Returns the client summary class
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <returns></returns>
        public ClientSummary GetSummary()
        {
            return mClientSummary;
        }


        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Adds a new chart object to the collection
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <param name="newChart"></param>
        public void AddChart(Chart newChart)
        {
            mCharts.Add(newChart);
        }

        /// <summary>
        /// -------------------------------------------------------------------------------
        /// Returns collection of chart objects
        /// -------------------------------------------------------------------------------
        /// </summary>
        /// <returns></returns>
        public ArrayList GetAllCharts()
        {
            return mCharts;
        }

        //-------------------------------------------------------------------------------
        // Private data region
        //-------------------------------------------------------------------------------
        ClientSummary mClientSummary;
        ClientAssessment mClientAssessment;
        ArrayList mCharts;
        static Utility DBUtil = new Utility();

    }
};
